﻿using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using VupSlashLauncher.Common;
using VupSlashLauncher.Model;
using VupSlashLauncher.ViewModel;

namespace VupSlashLauncher.View
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : Window
    {
        MainViewModel model = new MainViewModel();
        public MainView()
        {
            InitializeComponent();
            this.DataContext = model;
        }

        private void WindowMove(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (GlobalValues.PositionLeft != 0) this.Left = GlobalValues.PositionLeft;
            if (GlobalValues.PositionTop != 0) this.Top = GlobalValues.PositionTop;
        }

        private void Window_ContentRendered(object sender, System.EventArgs e)
        {
            WindowStyle = WindowStyle.SingleBorderWindow;
            ResizeMode = ResizeMode.CanResize;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (!model.IDownload.TaskOK && IMessageBox.Show("还有任务在进行中，真的要关闭启动器吗？", "关闭提示", "继续任务", "关闭窗口")) return;
            IFunc.WriteConfig("PositionLeft", this.Left.ToString());
            IFunc.WriteConfig("PositionTop", this.Top.ToString());
            model.SaveConfigFile();
            Close();
        }

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.Space)
            {
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
            {
                e.Handled = true;
                CloseButton_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                model.GoToView("StartView");
            }
            else if (e.Key == Key.F3)
            {
                if (GlobalValues.DebugMode == false)
                {
                    GlobalValues.DebugMode = true;
                }
                else
                {
                    GlobalValues.DebugMode = false;
                }
                IFunc.WriteConfig("Debug", GlobalValues.DebugMode.ToString());
            }

            GlobalValues.DetectWord += e.Key.ToString();
            if (GlobalValues.DetectWord.Contains("NEWUPDATEURL"))
            {
                GlobalValues.DetectWord = null;
                string temp = IInputBox.Show("输入游戏更新地址", "神秘窗口");
                if (!(temp == null)) INIOperation.INIWriteValue(GlobalValues.ConfigFile, "Vup", "CheckUpdateUrl", temp);
            }
            else if (GlobalValues.DetectWord.Contains("THEMECOLOR"))
            {
                if (IMessageBox.Show("您当前的主题颜色的十六进制码是\n" + GlobalValues.ThemeColor, "主题颜色", "复制"))
                    System.Windows.Clipboard.SetDataObject(GlobalValues.ThemeColor);
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Contains("MENGLONGSHAOZHU"))
            {
                IMessageBox.Show("萌龙少主永远滴神", "", "表示认可", "不敢苟同");
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Contains("CONFIGFILE"))
            {
                System.Diagnostics.Process.Start(GlobalValues.ConfigFile);
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Contains("TEST"))
            {
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Contains("DELETE"))
            {
                try
                {
                    IFunc.DeleteFile(IInputBox.Show("Delete"));
                }
                catch { }
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Contains("SHOWDOWNLOADBUTTON"))
            {
                model.ShowModel.IsDownloadButtonShow = true;
                GlobalValues.DetectWord = null;
            }
            else if (GlobalValues.DetectWord.Length > 500) GlobalValues.DetectWord = null;
        }

        private async void ShowDownloadView(object sender, RoutedEventArgs e)
        {
            if (DownloadView.Visibility == Visibility.Collapsed)
            {
                Storyboard sb = this.FindResource("Opacity0To1Animation") as Storyboard;
                model.IDownload.MainViewVisiability = "Visible";
                sb.Begin(DownloadView);
            }
            else if (DownloadView.Visibility == Visibility.Visible)
            {
                Storyboard sb = this.FindResource("Opacity1To0Animation") as Storyboard;
                sb.Begin(DownloadView);
                await Task.Delay(200);
                model.IDownload.MainViewVisiability = "Collapsed";
                if (model.IDownload.TaskOK) model.ShowModel.IsDownloadButtonShow = false;
            }
        }
    }
}
